package com.example.api.job.config;

import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler;

import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;

/**
 * @name: SchedulingConfig
 * @description:
 * @author: xw.s
 * @date: 2021/6/25-16:02
 */
@Configuration
//@ConfigurationProperties(prefix = )
public class SchedulingConfig {

    // 方法1 配置TaskScheduler
//    @Bean
//    public ThreadPoolTaskScheduler taskScheduler() {
//        ThreadPoolTaskScheduler threadPoolTaskScheduler = new ThreadPoolTaskScheduler();
//        threadPoolTaskScheduler.setPoolSize(10);
//        threadPoolTaskScheduler.setRemoveOnCancelPolicy(true);
//        threadPoolTaskScheduler.setThreadNamePrefix("schedule-pool-");
//        threadPoolTaskScheduler.setWaitForTasksToCompleteOnShutdown(true);
//        return threadPoolTaskScheduler;
//    }

    // 方法2 配置定时任务线程池
    // 该方法需要用到scheduling配置，否则还是单线程
    // 虽然参数：1，但是配置文件中是5，实际创建了多少以配置文件为准
    @Bean
    public ScheduledExecutorService scheduledExecutorService() {
        ScheduledExecutorService scheduledExecutorService = Executors.newScheduledThreadPool(1);
        return scheduledExecutorService;
    }
}
